package net.mikoo.seals.manage.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.ApplyServices;
import net.mikoo.seals.biz.service.DevicesActService;
import net.mikoo.seals.biz.service.DevicesServices;
import net.mikoo.seals.biz.service.DevicesTakeService;
import net.mikoo.seals.biz.service.DevicesUsedServices;
import net.mikoo.seals.biz.service.SealServices;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.biz.service.WorkflowService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.enums.SealStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.ApplyQuery;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesActQuery;
import net.mikoo.seals.model.DevicesQuery;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.DevicesTakeQuery;
import net.mikoo.seals.model.DevicesUsed;
import net.mikoo.seals.model.DevicesUsedCert;
import net.mikoo.seals.model.DevicesUsedQuery;
import net.mikoo.seals.model.Seal;
import net.mikoo.seals.model.SealQuery;
import net.mikoo.seals.model.Upfile;
import net.mikoo.seals.model.WorkflowSetting;

@Controller
@RequestMapping("/apply")
public class ApplyController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(ApplyController.class);

	@Autowired
	private ApplyServices applyServices;
	
	@Autowired
	private VerifyService verifyService;
	
	@Autowired
	private SealServices sealServices;
	
	@Autowired
	private DevicesServices devicesServices;
	
	@Autowired
	private DevicesActService devicesActService;
	
	@Autowired
	private DevicesUsedServices devicesUsedServices;
	
	
	@Autowired
	private DevicesTakeService devicesTakeService;
	
	@Autowired
	private WorkflowService workflowService;
	
	
	/**
	 * 测试申请
	 * @param devicesId
	 * @param model
	 * @return
	 */
	@RequestMapping("/test")
	public String testForm(Long devicesId,Model model) {
		
		Devices devices = devicesServices.getInfoById(devicesId);
		model.addAttribute("devices",devices);
		return "apply/testForm";
	}
	
	
	@RequestMapping("/list")
	public String list(ApplyQuery query,Model model,HttpServletRequest request) {
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		PageModel<Apply> pm = applyServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "apply/list";
	}
	
	/**
	 * 申请印章
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/seals")
	public String seals(SealQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setSealStatus(SealStatusEnum.ENABLE.code());
		query.setDevicesStatus(DevicesStatusEnum.ACTIVATE.code());
		
		PageModel<Seal> pm =  sealServices.getPageByQuery(query);
		
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "apply/seals";
	}
	
	
	@RequestMapping("/forms")
	public String forms(SealQuery query,@RequestParam(value="applyId",required = false)Long applyId,HttpServletRequest request,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		Seal seal = sealServices.getInfoByQuery(query);
		model.addAttribute("seal", seal);
		
		List<WorkflowSetting> settings = workflowService.getSettingsByWorkflowId(seal.getWorkflowId());
		model.addAttribute("settings", settings);
		
		if(seal.getSpecialWorkflowId() !=null) {
			List<WorkflowSetting> specials = workflowService.getSettingsByWorkflowId(seal.getSpecialWorkflowId());
			model.addAttribute("specials", specials);
		}
		
		if(applyId != null) {
			Apply apply = applyServices.getById(applyId);
			model.addAttribute("apply", apply);
		}
		
		return "apply/forms";
	}
	
	
	@RequestMapping("/info")
	public String info(ApplyQuery query,HttpServletRequest request,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		Apply  apply =  applyServices.getInfoByQuery(query);
		
		List<Upfile> upfiles = applyServices.getApplyfiles(apply.getApplyId());
		
		ApplyVerifyQuery vquery = new ApplyVerifyQuery();
		vquery.setCompanyId(companyId);
		vquery.setApplyId(apply.getApplyId());
		List<ApplyVerify> verifys = verifyService.getListByQuery(vquery);
		
		model.addAttribute("apply", apply);
		model.addAttribute("upfiles", upfiles);
		model.addAttribute("verifys", verifys);
		return "apply/info";
	}
	
	

	@RequestMapping("/submit")
	@ResponseBody
	public Map<String, Object> submit(Apply apply,HttpServletRequest request) {
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			apply.setCompanyId(companyId);
			apply.setPartyId(partyId);
			applyServices.apply(apply);
			
			return result(SUCCESS, "申请成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	/**
	 * 申请通过印章
	 * @param query
	 * @param request
	 * @param model
	 * @return
	 */
	
	@RequestMapping("/approve/list")
	public String approveList(DevicesActQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		PageModel<DevicesAct> pm = devicesActService.getActPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		
		return "apply/approveList";
	}
	
	/**
	 * 申请撤回
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("/retract")
	@ResponseBody
	public Map<String, Object> retract(ApplyQuery query,HttpServletRequest request) {
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			
			query.setCompanyId(companyId);
			query.setPartyId(partyId);
			applyServices.retract(query);
			
			return result(SUCCESS, "撤回成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	@RequestMapping("/approve/info")
	public String approveInfo(DevicesActQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
//		query.setCompanyId(companyId);
		//query.setCustosPartyId(partyId);
		DevicesAct devicesAct = devicesActService.getInfoByQuery(query);
		model.addAttribute("devicesAct", devicesAct);
		
		ApplyQuery applyQuery = new ApplyQuery();
		applyQuery.setCompanyId(companyId);
		applyQuery.setApplyId(devicesAct.getApplyId());
		Apply apply = this.applyServices.getInfoByQuery(applyQuery);
		model.addAttribute("apply", apply);
		
		List<Upfile> applyFiles = applyServices.getApplyfiles(devicesAct.getApplyId());
		model.addAttribute("applyFiles", applyFiles);
		
		ApplyVerifyQuery verifyQuery  = new ApplyVerifyQuery();
		verifyQuery.setApplyId(devicesAct.getApplyId());
		verifyQuery.setCompanyId(companyId);
		List<ApplyVerify> verifys = verifyService.getListByQuery(verifyQuery);
		model.addAttribute("verifys", verifys);
		
		
		DevicesUsedQuery usedQuery = new DevicesUsedQuery();
		usedQuery.setCompanyId(companyId);
		usedQuery.setDevicesActId(devicesAct.getDevicesActId());
		List<DevicesUsed> useds = devicesUsedServices.getListByQuery(usedQuery);
		model.addAttribute("useds", useds);
		
		
		List<DevicesUsedCert> usedCerts = devicesUsedServices.getCertsByDevicesActId(devicesAct.getDevicesActId());
		model.addAttribute("usedCerts", usedCerts);
		
		
		
		return "apply/approveInfo";
	}
	
	
	@RequestMapping("/take/list")
	public String takes(DevicesTakeQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);

		PageModel<DevicesTake> pm = devicesTakeService.getPageByQuery(query);

		model.addAttribute("pm", pm);
		return "apply/takeList";
	}
	
	@RequestMapping("/take/info")
	public String takeInfo(DevicesTakeQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		
		DevicesTake devicesTake = devicesTakeService.getInfoByQuery(query);
		model.addAttribute("devicesTake", devicesTake);
		
		DevicesQuery devicesQuery = new DevicesQuery();
		devicesQuery.setDevicesId(devicesTake.getDevicesId());
		devicesQuery.setCompanyId(companyId);
		Devices devices = devicesServices.getInfoByQuery(devicesQuery);
		model.addAttribute("devices", devices);
		return "apply/takeInfo";
	}
	
	@RequestMapping("/used/list")
	public String usedList(DevicesUsedQuery query, HttpServletRequest request, Model model) {

		Long partyId = getCurrentPartyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		query.setPartyId(partyId);
		PageModel<DevicesUsed> pm = devicesUsedServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "apply/usedList";
	}
	
	@RequestMapping("/used/info")
	public String usedInfo(DevicesUsedQuery query, HttpServletRequest request, Model model) {

		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		
		DevicesUsed devicesUsed = devicesUsedServices.getInfoByQuery(query);
		List<DevicesUsedCert> usedCerts = devicesUsedServices.getCertsByQuery(query);
		model.addAttribute("used", devicesUsed);
		model.addAttribute("usedCerts", usedCerts);
		return "apply/usedInfo";
	}
	

}
